package app;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import model.*;
import org.pushingpixels.substance.api.skin.SubstanceSaharaLookAndFeel;
import view.*;

import javax.sql.DataSource;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.util.Enumeration;
import java.util.List;

public class App {

    public static DataSource dataSource;
    public static User loggedUser;
    public static List<User> currentUserList;
    public static List<Product> currentProductList;
    public static List<StockIn> currentStockInList;
    public static List<StockOut> currentStockOutList;
    public static List<SaleRecord> currentSaleRecordList;
    public static SystemPane systemPane;
    public static WarehousePane warehousePane;
    public static StatisticsPane statisticsPane;
    public static SaleView saleView;

    //验证管理权限
    public static boolean checkManagePermission() {
        return loggedUser.getJobId() == 0;
    }
    //验证销售权限
    public static boolean checkSalePermission() {
        return loggedUser.getJobId() == 0 || loggedUser.getJobId() == 1;
    }
    //验证仓管权限
    public static boolean checkWareHousePermission() {
        return loggedUser.getJobId() == 0 || loggedUser.getJobId() == 2;
    }

    public static void main(String[] args) {

        //初始化JDBC连接池
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/supermarket");
        config.setUsername("root");
        config.setPassword("root");
        config.addDataSourceProperty("connectionTimeout", "1000"); // 连接超时：1秒
        config.addDataSourceProperty("idleTimeout", "60000"); // 空闲超时：60秒
        config.addDataSourceProperty("maximumPoolSize", "10"); // 最大连接数：10
        dataSource = new HikariDataSource(config);

        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        UIManager.put( "TabbedPane.tabInsets", new Insets(10,30,10,30) );

        //统一所有子窗口设置字体样式
        FontUIResource fontRes = new FontUIResource(new Font("微软雅黑",Font.PLAIN,14));
        for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontRes); UIManager.put(key, fontRes);
            }
        }

        SwingUtilities.invokeLater(() -> {
            try {
                //设置主题样式
                UIManager.setLookAndFeel(new SubstanceSaharaLookAndFeel());
            } catch (Exception e) {
                System.out.println("Substance Graphite failed to initialize");
            }
            //新建登陆界面
            new LoginView();
        });
    }

    public static void notifySaleRecordListChanged() {
        saleView.notifySaleRecordListChanged();
        statisticsPane.notifySaleRecordListChanged();
        warehousePane.notifyOutHistoryDataChanged();
    }
}
